  
var UserStats = Class.create({

  initialize: function(url, properties){    
    this.userID = properties.userID;
    this.positions = null;
    this.elementID = properties.elementID;
    if(properties.width!=null)
      this.width = properties.width;
    else
      this.width = null;
    this.height = properties.height;
     
    this.ar = new Ajax.Request(url, {
      method: 'get',
      onSuccess: this.parseResponse.bind(this),
      onLoading: this.showPreloader()
    });
    
    this.data = null;
    this.graph = null;
  },
    
  getUserID: function(){
    return this.userID;
  },
  
  setUserID: function(userID){
    this.userID = userID;
  },
  
  getPositions: function(){
    return this.position;
  },
  
  setPositions: function(positions){
    this.positions = positions;
  },    
  
  getElementID: function(){
    return this.elementID;
  },
  
  setElementID: function(elementID){
    this.elementID = elementID;
  },  
  
  showPreloader: function(){
    $(this.elementID).update("");
    var waitingThing = document.createElement('img');
    waitingThing.style.display = 'none';
    waitingThing.setAttribute('src', 'js/45.gif');
    waitingThing.alt = 'loading...';    
    $(waitingThing).appear();
    $(this.elementID).update(waitingThing);
  },
  
  parseResponse: function(transport){
    var tmp = transport.responseText;
    var tmpJson = eval('(' + transport.responseText +')')    
    this.drawGraph(tmpJson);    
  },
  
  generateData: function(json){
    var values = json.userStats;   
    this.positions = values;  
    var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'ranking position');                   
        data.addColumn('number', 'SWAT');
        data.addRows(json.userStats.length);                  
    for(i=0; i<values.length; i++){
      data.setValue(i, 0, values[i].generated);
      data.setValue(i, 1, parseInt(values[i].position));
      data.setValue(i, 2, 50);
    }    
    this.data = data;                            
  },
  
  drawGraph: function(json){
    this.generateData(json);
    var chart = new google.visualization.AreaChart($(this.elementID).update("")); 
    this.chart = chart;
    if(this.height==null || isNaN(this.height)){     
      var myWidth = 0, myHeight = 0;
      if( typeof( window.innerHeight ) == 'number' ) {
        //Non-IE
        //this.height = $(this.elementID).previous().height -10;        
        this.height = window.innerHeight -200;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        this.height = document.documentElement.clientHeight -200;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        this.height = document.body.clientHeight -200;
      }
      
      if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        //this.height = $(this.elementID).previous().height -10;        
        this.width = window.innerWidth - 400;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        this.width = document.documentElement.clientWidth -400;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        this.width = document.body.clientWidth -400;
      }  
      
    }    
    chart.draw(this.data, {width: this.width, height: this.height, title: this.userID + ' Stats', legend: 'bottom'});            
  }

  
  /*
  reDrawChart: function(elementID, properties){
    var chart = new google.visualization.AreaChart($(elementID).update(""));  
    if(this.data!=null){
      chart.draw(this.data, {width: this.width*2, height: this.height*2, title: this.userID + ' Stats 2x', legend: 'none'});
    }else{
      $(elementID).update('<span style="color:red">ERROR</span>');
    }  
  }
  */
});
